﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.Windows.Forms;
using MES.DataGateway.SyncEntity;
using MES.DataGateway.Common;
using MES.DataGateway.OracleDAL;
using MES.DataGateway.SyncCommon;
using MES.DataGateway.SyncInterface;
using MES.DataGateway.StandardEntity;
using System.IO;
using System.Configuration;

namespace MES.DataGateway.StandardDataSync
{
    public class CommTool : SyncInfterface
    {

        private TreeView trvMes = new TreeView();
        private TreeView trvEnterprise = new TreeView();
        private ServiceEntity m_ServiceInfoEntity = null;
        private string m_ProjectID = "";
        private string m_LibID = "";
        private int m_CryNumber = 0;
        private string m_DataFilePath = "";
        private OutInfoEntity m_OutParameterObject = null;
        private List<OutInfoEntity> m_OutParameterObjectList = new List<OutInfoEntity>();
        private string m_AccessoriesPath = "";
        private string m_SyncType = "1";
        private string m_StrDocumentName = "";
        private string m_serviceType = "CommTool";

        #region 属性
        public string AccessoriesPath
        {
            set { m_AccessoriesPath = value; }
        }

        public OutInfoEntity OutParameterObject
        {
            get { return m_OutParameterObject; }
        }

        public List<OutInfoEntity> OutParameterObjectList
        {
            get { return m_OutParameterObjectList; }
        }

        public ServiceEntity ServiceInfoEntity
        {
            get { return m_ServiceInfoEntity; }
            set { m_ServiceInfoEntity = value; }
        }

        public string ProjectID
        {
            get { return m_ProjectID; }
            set { m_ProjectID = value; }
        }

        public string LibID
        {
            get { return m_LibID; }
            set { m_LibID = value; }
        }

        public string DataFilePath
        {
            get { return m_DataFilePath; }
            set { m_DataFilePath = value; }
        }
        #endregion


        #region 获取循环节点的循环次数
        private void GetCryNodeNumber(string nodeFullPath)
        {
            foreach (TreeNode node in trvEnterprise.Nodes)
            {
                if (node.FullPath == nodeFullPath)
                {
                    m_CryNumber = m_CryNumber + 1;
                }
                else
                {
                    GetCryChidlNodeNumber(nodeFullPath, node);
                }
            }
        }

        private void GetCryChidlNodeNumber(string nodeFullPath, TreeNode childNodes)
        {
            foreach (TreeNode node in childNodes.Nodes)
            {
                if (node.FullPath == nodeFullPath)
                {
                    m_CryNumber = m_CryNumber + 1;
                }
                else
                {
                    GetCryChidlNodeNumber(nodeFullPath, node);
                }
            }
        }
        #endregion

        #region 根据映射条件查找对应的值
        private string SearchMappingValue(string nodeFullPath, TreeNode eNode)
        {
            string strRturnValue = "";
            if (eNode.Nodes.Count > 0)
            {
                foreach (TreeNode node in eNode.Nodes)
                {
                    if (node.FullPath.ToLower() == nodeFullPath.ToLower())
                    {
                        if (node.Tag == null)
                        {
                            strRturnValue = "";
                            break;
                        }
                        else
                        {
                            strRturnValue = node.Tag.ToString().Trim();
                            break;
                        }
                    }
                }
            }
            return strRturnValue;
        }

        private string SearchChildMappingValue(string nodeFullPath, TreeNode childNodes)
        {
            string strRturnValue = "";
            foreach (TreeNode node in childNodes.Nodes)
            {
                if (node.FullPath == nodeFullPath)
                {
                    if (node.Tag == null)
                    {
                        strRturnValue = "";
                        break;
                    }
                    else
                    {
                        strRturnValue = node.Tag.ToString().Trim();
                        break;
                    }
                }
                else
                {
                    strRturnValue = SearchChildMappingValue(nodeFullPath, node);
                    if (strRturnValue.Trim() != "")
                    {
                        break;
                    }
                }
            }
            return strRturnValue;
        }
        #endregion

        #region 循环取相关节点值
        private string GetCryValue(string resourceFullPath, int intIndex)
        {
            string strReturnValue = "";
            List<string> valueList = new List<string>();

            foreach (TreeNode node in trvEnterprise.Nodes)
            {
                if (node.FullPath == resourceFullPath)
                {
                    string strNodeValue = "";
                    if (node.Tag != null)
                    {
                        strNodeValue = node.Tag.ToString().Trim();
                    }
                    valueList.Add(strNodeValue);
                }
                else
                {
                    GetChildCryValue(resourceFullPath, node, valueList);
                }
            }

            for (int i = 0; i < valueList.Count; i++)
            {
                if (i == intIndex)
                {
                    strReturnValue = valueList[i];
                }
            }
            return strReturnValue;
        }

        private void GetChildCryValue(string resourceFullPath, TreeNode parentNode, List<string> valueList)
        {
            foreach (TreeNode node in parentNode.Nodes)
            {
                if (node.FullPath == resourceFullPath)
                {
                    string strNodeValue = "";
                    if (node.Tag != null)
                    {
                        strNodeValue = node.Tag.ToString().Trim();
                    }
                    valueList.Add(strNodeValue);
                }
                else
                {
                    GetChildCryValue(resourceFullPath, node, valueList);
                }
            }
        }
        #endregion

        #region 导入数据文件
        private void LoadXmlData()
        {
            string strFilter = string.Format("ProjectID='{0}' and libID='{1}'", m_ServiceInfoEntity.ProjectID, m_LibID);
            DataRow[] rows = m_ServiceInfoEntity.ServiceTable.Select(strFilter);

            CreateMesTemple(Application.StartupPath + "\\XML\\Template\\MES\\" + rows[0]["MesTemplate"].ToString().Trim());
            LoadEnterpriseXML(m_DataFilePath);
        }

        private void LoadEnterpriseXML(string strPath)
        {
            trvEnterprise.Nodes.Clear();
            XmlDocument myxml = new XmlDocument();
            myxml.Load(strPath);

            XmlNode stu = myxml.DocumentElement;
            foreach (XmlNode node in stu.ChildNodes)
            {
                TreeNode childNodes = new TreeNode();
                childNodes.Text = node.Name;
                childNodes.Tag = node.InnerText;
                GetChildXmlNode(node, childNodes);
                trvEnterprise.Nodes.Add(childNodes);
            }
        }

        private void GetChildXmlNode(XmlNode nodes, TreeNode childTreeNode)
        {
            if (nodes.ChildNodes.Count == 0)
            {
                return;
            }
            else
            {
                foreach (XmlNode childNode in nodes.ChildNodes)
                {
                    TreeNode myNode = new TreeNode();
                    myNode.Text = childNode.Name;
                    myNode.Tag = childNode.InnerText;
                    GetChildXmlNode(childNode, myNode);
                    childTreeNode.Nodes.Add(myNode);
                }
            }
        }

        private void CreateMesTemple(string strFileName)
        {
            trvMes.Nodes.Clear();
            XmlDocument myxml = new XmlDocument();
            myxml.Load(strFileName);
            if (myxml.DocumentElement == null)
            {
                return;
            }
            XmlNode stu = myxml.DocumentElement;
            foreach (XmlNode node in stu.ChildNodes)
            {
                TreeNode childNodes = new TreeNode();
                childNodes.Text = node.Name;
                if (node.Attributes.GetNamedItem("Type") != null)
                {
                    childNodes.Tag = node.Attributes.GetNamedItem("Type").Value;
                }
                else
                {
                    childNodes.Tag = "";
                }
                GetMesChildXmlNode(node, childNodes);
                trvMes.Nodes.Add(childNodes);
            }
        }

        private void GetMesChildXmlNode(XmlNode nodes, TreeNode childTreeNode)
        {
            if (nodes.ChildNodes.Count == 0)
            {
                return;
            }
            else
            {
                foreach (XmlNode childNode in nodes.ChildNodes)
                {
                    TreeNode myNode = new TreeNode();
                    myNode.Text = childNode.Name;
                    if (childNode.Attributes.GetNamedItem("Type") != null)
                    {
                        myNode.Tag = childNode.Attributes.GetNamedItem("Type").Value;
                    }
                    else
                    {
                        myNode.Tag = "";
                    }
                    GetMesChildXmlNode(childNode, myNode);
                    childTreeNode.Nodes.Add(myNode);
                }
            }
        }
        #endregion

        #region 查找单节点映射值
        private string GetMappingValue(TreeNode node, TreeNode childNodes)
        {
            string strValue = "";
            string strFilter = string.Format("projectID='{0}' and LibID='{1}' and mesFileld='{2}'", m_ProjectID, m_LibID, node.FullPath);
            DataRow[] mappingRows = m_ServiceInfoEntity.MappingTable.Select(strFilter); //映射条件登记
            if (mappingRows.Length > 0)
            {
                strValue = SearchMappingValue(mappingRows[0]["EnterpriseFileld"].ToString().Trim(), childNodes);
            }
            return strValue;
        }
        #endregion

        #region 查找目标文件映射路径
        private string GetMappingPath(TreeNode node)
        {
            string strFilter = string.Format("projectID='{0}' and LibID='{1}' and mesFileld='{2}'", m_ProjectID, m_LibID, node.FullPath);
            DataRow[] mappingRows = m_ServiceInfoEntity.MappingTable.Select(strFilter); //映射条件登记
            if (mappingRows.Length > 0)
            {
                return mappingRows[0]["EnterpriseFileld"].ToString().Trim();
            }
            else
            {
                return "";
            }
        }
        #endregion

        #region SyncInfterface 成员

        public void Execute()
        {
            LoadXmlData();
            string documentSetName = ""; //LoadDocumentSet();
            LoadCommTool(documentSetName);            
        }

        private void LoadCommTool(string documentSetName)
        {
            string SpecAndRev = "";

            SyncParameterEntity mainEntity = new SyncParameterEntity();
            mainEntity.OperationKind = "New";
            mainEntity.ServiceKey = m_serviceType;

            mainEntity.DataList = new List<DataEntity>();
            StandardDAL dal = new StandardDAL();
            SyncParameterEntity productEntity = new SyncParameterEntity();

            foreach (TreeNode eNode in trvEnterprise.Nodes[4].Nodes[2].Nodes)
            {
                foreach (TreeNode childeNode in eNode.Nodes)
                {
                    if (childeNode.Text != "CommTools")
                    {
                        continue;
                    }

                    foreach (TreeNode specialNode in childeNode.Nodes)
                    {
                        mainEntity.DataList.Clear();
                        foreach (TreeNode node in trvMes.Nodes)
                        {
                            if (node.Text != m_serviceType)
                            {
                                continue;
                            }
                            foreach (TreeNode wChildNode in node.Nodes)  //workFlow下面业务数据处理
                            {
                                string strValue = GetMappingValue(wChildNode, specialNode);

                                if (wChildNode.Tag.ToString() == "dataField" || wChildNode.Tag.ToString() == "namedObjectField")
                                {
                                    DataEntity entity = new DataEntity();
                                    entity.FieldName = wChildNode.Text;
                                    entity.FieldType = wChildNode.Tag.ToString().Trim();
                                    entity.FieldValue = strValue;
                                    entity.FileldVersion = "";
                                    mainEntity.DataList.Add(entity);
                                }
                                else if (wChildNode.Text == "Spec")
                                {
                                    DataEntity entity = new DataEntity();
                                    entity.FieldName = wChildNode.Text;
                                    entity.FieldType = wChildNode.Tag.ToString().Trim();

                                    foreach (TreeNode SpecNode in wChildNode.Nodes)
                                    {
                                        if (SpecNode.Text == "Name")
                                        {
                                            entity.FieldValue = eNode.Nodes[2].Tag.ToString().Trim(); //GetMappingValue(SpecNode);
                                        }
                                        else if (SpecNode.Text == "Revision")
                                        {
                                            entity.FileldVersion = eNode.Nodes[1].Tag.ToString().Trim();  // GetMappingValue(SpecNode);
                                        }
                                    }
                                    SpecAndRev = entity.FieldValue + "/" + entity.FileldVersion;
                                    mainEntity.DataList.Add(entity);
                                }
                            }
                            foreach (DataEntity NdoName in mainEntity.DataList)
                            {
                                if (NdoName.FieldType != "namedObjectField")
                                {
                                    continue;
                                }
                                if (NdoName.FieldValue.ToString().Trim() != "")
                                {
                                    ProcessNDOEntity(NdoName.FieldName, NdoName.FieldName + "name", NdoName.FieldValue.ToString().Trim());
                                }
                            }
                            ExecuteAPI exectAPI = new ExecuteAPI();
                            OutInfoEntity infoEntity = new OutInfoEntity();
                            exectAPI.CreateXACommonTool (mainEntity, ref infoEntity);
                        }
                    }
                }
            }
        }


        private void ProcessNDOEntity(string strTable, string strName, string strValue)
        {
            StandardDAL dal = new StandardDAL();
            DataSet ds = dal.GetNDOTable(strTable, strName, strValue);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return;
            }
            else
            {
                OutInfoEntity infoEntity = new OutInfoEntity();
                ExecuteAPI exectAPI = new ExecuteAPI();
                exectAPI.CreateNDOData(strTable, strValue, ref infoEntity);
                m_OutParameterObjectList.Add(infoEntity);
            }
        }
        public void ShowAttribute()
        {
            
        }
        public string SyncType
        {
            get { return m_SyncType; }
            set { m_SyncType = value; }
        }

        #endregion
    }
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       